package indi.yuluo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import indi.yuluo.entity.DTO.ProblemDiscussDto;
import indi.yuluo.entity.problem.Problem;
import indi.yuluo.service.DiscussService;
import indi.yuluo.service.ProblemService;
import indi.yuluo.vo.Msg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

import static indi.yuluo.Enum.Constant.PER_PAGE_NUMBER;

/**
 * @Author: yuluo
 * @CreateTime: 2022-08-19  21:56
 * @Description: TODO
 */

@RestController
@RequestMapping(value = "/problem")
@Slf4j
@Api("oi题目相关类")
public class ProblemController {

    @Resource
    private ProblemService problemService;

    @Resource
    private DiscussService discussService;

    /**
     * 分页查询 显示题目信息
     * @return
     */
    @GetMapping("/getProblemInfo")
    @ApiOperation(value = "获取数据库中的题目信息", httpMethod = "GET", notes = "postman接口测试通过")
    public Msg getQuestionInfo(
            @RequestParam(value = "pageNums", defaultValue = "1")Integer pageNums,
            HttpServletRequest request
    ) {

        // 需要放到session域中的数据
        // 重新清空session域中的值，重新放入
        request.getSession().removeAttribute("prePageNumberInfo");
        PageHelper.startPage(pageNums, PER_PAGE_NUMBER.getNumber());
        List<Problem> list = problemService.getQuestionInfo();
        PageInfo<Problem> page = new PageInfo<>(list, PER_PAGE_NUMBER.getNumber());
        // 将数据放到会话域中
        request.getSession().setAttribute("prePageNumberInfo", page);

        // 需要返回到前端的数据
        // 返回结果
        List<ProblemDiscussDto> problemDiscussDtoList;

        // 根据pid去后端查询，将数据封装到Dto中，返回给前端
        problemDiscussDtoList = list.stream().map((item) -> {

            ProblemDiscussDto problemDiscussDto = new ProblemDiscussDto();
            BeanUtils.copyProperties(item, problemDiscussDto);
            String pid = item.getProblemId();

            int discussNum = discussService.getDiscussNumByProblemId(pid);
            problemDiscussDto.setDiscussNum(discussNum);

            return problemDiscussDto;
        }).collect(Collectors.toList());

        return Msg.success().add("prePageNumberInfo", problemDiscussDtoList);
    }

    /**
     * 通过题目id获取题目信息
     * @param id
     * @return
     */
    @GetMapping("/getProblemByProblemId")
    @ApiOperation(value = "通过题目id获取题目信息", httpMethod = "GET")
    public Msg getProblemByProblemId(@RequestParam(value = "id") Long id) {

        Problem problem = problemService.getProblemById(id);

        return Msg.success().add("problem", problem);
    }

}
